
<body>
<ul class="table-list">
    <?php echo $tables_html?>
</ul>

<div class="popup-form">


</div>

<div class="search-form">
    <div class="input">
        <input type="text" id="search-input" onkeypress="search_product($(this).val())"/>
        <div class="result">
            <div class="scroll-pane" style="width: 200px">
            <ul id="result-list">
                <?php foreach($products as $item): ?>
                    <li>
                        <a class="product-result-item" product_id="<?php echo $item->id?>" href="#"><?php echo $item->name?></a>
                        <div class="price"><?=$item->formatPrice?></div>
                    </li>
                <?php endforeach ?>
            </ul>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

    function add_order()
    {
        $.ajax({
            url: '<?php echo base_url()?>home/add-order',
            data : {table_id: currentTable},
            type:'POST',
            dataType:'json',
            success:function(data){
                if(data.status)
                {
                    alert('Đã nhập vào hoá đơn');
                    var windowSizeArray = [ "width=300,height=600",
                        "width=300,height=400,scrollbars=no" ];
                    var url = '<?=base_url()?>home/print/' + data.order_id;
                    var windowName = "Print";//$(this).attr("name");
                    var windowSize = windowSizeArray[0];

                    window.open(url, windowName, windowSize);

                    event.preventDefault();
                }
                alert('Nhập vào bị lỗi');

            }

        })
    }

    function get_table(id)
    {
        currentTable = id;
        $.ajax({
            url: '<?php echo base_url()?>home/get-table',
            data : {id : id},
            type:'POST',
            dataType:'json',
            success:function(data){
                $('.popup-form').html(data);
                $('.search-form').show('fast');
                reload_store_data();
                init_product_buttons();
                $('html,body').scrollTop(0);
            }

        })
    }

    function search_product(content)
    {
        $('#result-list').children().remove();

            $.ajax({
            url: '<?php echo base_url()?>home/search-product',
            data : {content : content},
            type:'POST',
            dataType:'json',
            success:function(data){
                for(var i=0 ; i<data.length ; i++)
                {
                    var item = $('<li>');
                    item.html('<a href="#" class="product-result-item" product_id='+data[i].id+' >'+data[i].name+'</a>');
                    $('#result-list').append(item);
                }
                init();

            }

        })
    }

    function add_product(id)
    {
        var quantity = get_quantity();
        if(!quantity) return;
        $.ajax({
            url: '<?php echo base_url()?>home/add-product',
            data : {table_id:currentTable,product_id:id,quantity:quantity},
            type:'POST',
            dataType:'json',
            success:function(data){
                if(data)
                {
                    reload_store_data();
                    reload_tables_list();
                }
            }

        });

    }

    function reload_store_data()
    {
        $('.contacts .content-list').remove();
        $.ajax({
            url: '<?php echo base_url()?>home/reload-store',
            data : {table_id:currentTable},
            type:'POST',
            dataType:'json',
            success:function(data){
//                alert(data);
                var storeTotal = 0;
                $.each(data,function(i){
                    var current_item = listObject[data[i].product_id];
                    var current_total = current_item.price * data[i].quantity;
                    storeTotal += current_total;
                        var row = $('<tr class="content-list" product_id="'+current_item.id+'"></tr>');
                        row.append('<td class="name" width="200px"><a href="#">'+current_item.name+'</a></td>');
                        row.append('<td class="unit" width="30px"><a href="#">'+current_item.unit+'</a></td>');
                        row.append('<td class="quantity" width="100px"><a href="#">'+data[i].quantity+'</a></td>');
                        row.append('<td class="price" width="100px"><a href="#">'+current_item.price+'</a></td>');
                        row.append('<td class="totalPrice" width="100px"><a href="#">'+current_total+'</a></td>');
                        row.append($('<td class="contact" width="100px">' +
                                '<input type="button" class="addButton" product_id='+current_item.id+' value="+" />' +
                                '<input type="button" class="subButton" product_id='+current_item.id+' value="-" />' +
                                '</td>'));
                        $('.contacts').append(row);

                });
                $('.storeTotal').text(storeTotal);

                init_product_buttons();
            }

        });
    }

    function get_quantity()
    {
        return prompt('Nhập số lượng','1');
    }

    function init_product_buttons()
    {
        $('.addButton').click(function(){
            var quantity = get_quantity();
            var id = $(this).attr('product_id');
            increaseProductNumber(id,quantity);

        });

        $('.subButton').click(function(){
            var quantity = get_quantity();
            var id = $(this).attr('product_id');
            decreaseProductNumber(id,quantity);

        });

    }

    function increaseProductNumber(id,quantity)
    {
        $.ajax({
            url: '<?php echo base_url()?>home/increase',
            data : {table_id:currentTable,product_id:id,quantity:quantity},
            type:'POST',
            dataType:'json',
            success:function(data){

                if(data) {
                    reload_store_data();
                }

            }

        });
    }

    function decreaseProductNumber(id,quantity)
    {
        $.ajax({
            url: '<?php echo base_url()?>home/decrease',
            data : {table_id:currentTable,product_id:id,quantity:quantity},
            type:'POST',
            dataType:'json',
            success:function(data){

                if(data) {
                    reload_store_data();
                }

            }

        });
    }

    function init()
    {
        $('.product-result-item').click(function(){
            add_product($(this).attr('product_id'));
        });


    }

    function clear_all()
    {
        if(!confirm('Có chắc muốn xoá ?')) return false;
        $.ajax({
            url: '<?php echo base_url()?>home/clear-all',
            data : {table_id:currentTable},
            type:'POST',
            dataType:'json',
            success:function(data){
                if(data) {
                    $('.contacts .content-list').remove();
                    $('.storeTotal').text('');
                    reload_tables_list();
                }

            }

        });
    }

    function reload_tables_list()
    {
        $.ajax({
            url: '<?php echo base_url()?>home/reload-table-list',
            data : {table_id:currentTable},
            type:'POST',
            dataType:'json',
            success:function(data){
//                $('.table-list').html('');
                $('.table-list').html(data.toString());
            }

        });
    }



    $(function(){
        init();
        init_product_buttons();

        listObject = {};
        currentTable = '';
<!--        add products to array-->
        <?php foreach($products as $item): ?>
            var item = {};
            item['id'] = '<?=$item->id?>';
            item['name'] = '<?=$item->name?>';
            item['unit'] = '<?=$item->unit?>';
            item['price'] = '<?=$item->price?>';
            listObject['<?=$item->id?>'] = item;
        <?php endforeach ?>

    });

</script>